using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Infrastructure.Database.DataSeeding;

public static class DataSeederExtensions
{
    public static async Task<IApplicationBuilder> SeedDataAsync(this IApplicationBuilder app) 
    {
        using IServiceScope scope = app.ApplicationServices.CreateScope();
        IEnumerable<IDataSeeder> dataSeeders = scope.ServiceProvider.GetServices<IDataSeeder>();

        foreach (IDataSeeder dataSeeder in dataSeeders)
        {
            await dataSeeder.SeedAsync();
        }
        return app;
    }
}
